﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.Entitys;
using Webcar.BLL;
using Webcar.Utils;
using Webcar.Controllers;
using System.Data.Objects.SqlClient;
using System.Web.Routing;
using System.Data;
using System.Reflection;
namespace Webcar.BLL
{
    public static class UserDataProvider
    {
        public static IQueryable GetCarList(string dept_code,string type,ShowCarOptions options)
        {
            if (options == null)
            {
                options = new ShowCarOptions() { ShowNormal = true };
            }
            var insure_list = from p in WebcarDataProvider.DB.BS_INSURE
                              group p by p.CAR_NO into result
                              select new { CAR_NO = result.Key, INSURE_DATE = result.Max(a => a.END_DATE) };
            List<string> filter = new List<string>();  
            if (options.ShowNormal)
                filter.Add("T");
            if (options.ShowDiscard)
                filter.Add("F");
            if (options.ShowDelete)
                filter.Add("D");

            var list = from car in WebcarDataProvider.DB.BS_CAR_RECORD
                       //join dept in CurrentUser.UserGrantedDept on car.CHILD_DEPT equals dept.DEPTCODE
                       join insure in insure_list on car.CAR_NO equals insure.CAR_NO
                       into result
                       where filter.Contains(car.IS_USE)//car.IS_USE == "T"
                       from bb in result.DefaultIfEmpty()
                       select new
                       {
                           CAR_NO = car.CAR_NO,
                           RECORD_NO = car.RECORD_NO,
                           DEPT_CODE = car.DEPT_CODE,
                           car_class_name = car.car_class_name,
                           RUN_DEPT = car.RUN_DEPT,
                           BUY_DATE = car.BUY_DATE,
                           VALUE = car.VALUE,
                           GUEST_NUM = car.GUEST_NUM,
                           CHILD_DEPT = car.CHILD_DEPT,
                           CAR_USE = car.CAR_USE,
                           IS_USE = car.IS_USE,
                           IS_TZ = car.car_class2 == "TZ" ? true : false,
                           IS_TZ_CN = car.car_class2 == "TZ" ? "是" : "否",
                           INSURE_DATE = bb.INSURE_DATE,
                           CAR_SORT = car.CAR_SORT
                       };
            if (type == "Moto")
            {
                list = list.Where(a => a.CAR_SORT=="52");
            }
            else
            {
                list = list.Where(a => a.CAR_SORT != "52");
            }
            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.CHILD_DEPT equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret, QuerySettings.CarList);
        }
        public static IQueryable GetInsureList(RouteData data)
        {
            var options = HttpContext.Current.Session["ShowInsureOptions"] as ShowInsureOptions;
            if (options == null) options = new ShowInsureOptions();
            var last_insure_list = WebcarDataProvider.DB.BS_INSURE.AsQueryable();
            if (options.ShowLast)
            {
                var max_insure_list = (from p in WebcarDataProvider.DB.BS_INSURE
                                    group p by p.CAR_NO into result
                                    select new { CAR_NO = result.Key, END_DATE = result.Max(a => a.END_DATE) });
                last_insure_list = from p in WebcarDataProvider.DB.BS_INSURE
                                   join p2 in max_insure_list
                                       on new { p.CAR_NO, p.END_DATE } equals new { p2.CAR_NO, p2.END_DATE }
                                   select p;
            }

            string dept_code = data.Values["id"].ToSafeString();
            string type = data.Values["type"].ToSafeString();
            var list = from p in last_insure_list
                       join p2 in CurrentUser.UserGrantedDept
                       on p.DEPT_CODE equals p2.deptcode
                       join p3 in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.CAR_NO equals p3.CAR_NO
                       where p3.IS_USE=="T"
                       select new {
                           INSURE_NO = p.INSURE_NO,
                           INSURE_NO2 = p.INSURE_NO2,
                           RECORD_NO = p3.RECORD_NO,
                           START_DATE = p.START_DATE,
                           END_DATE = p.END_DATE,
                           END_MONTH = SqlFunctions.DateName("year", p.END_DATE) + "-" + SqlFunctions.DateName("month", p.END_DATE),
                           INSURE_MONEY = p.INSURE_MONEY,
                           STATUS = DateTime.Now > p.END_DATE ? "已过期" : "执行中",
                           DEPT_CODE=p.DEPT_CODE,
                           child_dept = p3.CHILD_DEPT,
                           IsMoto = p3.CAR_SORT=="52"?true:false
                       };
            if (type == "Moto")
            {
                list = list.Where(a => a.IsMoto);
            }
            else
            {
                list = list.Where(a => !a.IsMoto);
            }

            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.child_dept equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret,QuerySettings.InsureList);
        }
        public static IQueryable GetCarList(RouteData routedata)
        {
            string dept_code = routedata.Values["id"].ToSafeString();
            string type = routedata.Values["type"].ToSafeString();

            var options = HttpContext.Current.Session["ShowCarOptions"] as ShowCarOptions;
            return GetCarList(dept_code,type,options);
        }
        public static IQueryable GetSecList(RouteData data)
        {
            string dept_code = data.Values["id"].ToSafeString();
            var list = from p in WebcarDataProvider.DB.SEC_RECORD
                       join p2 in WebcarDataProvider.DB.BS_EMP
                           on p.DRIVER equals p2.MAN_CODE
                           into result
                       from bb in result.DefaultIfEmpty()
                        join car in WebcarDataProvider.DB.BS_CAR_RECORD
                        on p.CAR_NO equals car.CAR_NO
                       select new { 
                           sec_no=p.SEC_NO,
                           dept_code=p.SEC_DEPT,
                           record_no = car.RECORD_NO,
                           driver_name= bb.MAN_NAME==null?p.DRIVER:bb.MAN_NAME,
                           child_dept = car.CHILD_DEPT,
                           loss_money = p.LOSS_MONEY,
                           sec_date = p.SEC_DATE,
                           sec_type=p.SEC_TYPE,
                           sec_kind = p.SEC_KIND,
                           sec_duty = p.SEC_DUTY,
                           sec_place= p.SEC_PLACE,
                           end_flag = p.IS_END=="T"?"已结案":"未结案"
                       };
            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.child_dept equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret,QuerySettings.SecList);
        }
        public static IQueryable GetSendList(this WebViewPage page)
        {
            string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            var list = from p in WebcarDataProvider.DB.BS_CAR_SEND
                       join car in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.car_no equals car.CAR_NO
                       into result
                       from bb in result.DefaultIfEmpty()
                       select new
                       {
                           xh=p.XH,
                           dept_code=p.DEPT_CODE,
                           child_dept = p.dept2,
                           place=p.PLACE,
                           start_date=p.START_DATE,
                           end_date=p.END_DATE,
                           check_man=p.CHECK_MAN,
                           send_man =p.send_man,
                           reason = p.REASON,
                           record_no = bb.RECORD_NO,
                           driver_name = p.driver_name,
                           is_check=p.is_check
                       };
            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.child_dept equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret, QuerySettings.CarSendList);
        }
        public static IQueryable GetDriverList(RouteData data)
        {
            //string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            string dept_code = data.Values["id"].ToSafeString();
            string type = data.Values["type"].ToSafeString();
            var options = HttpContext.Current.Session["ShowDriverOptions"] as ShowCarOptions;
            List<string> filter = new List<string>();
            if (options.ShowNormal)
                filter.Add("T");
            if (options.ShowDelete)
                filter.Add("F");
            var list = from p in WebcarDataProvider.DB.BS_EMP
                       join p4 in WebcarDataProvider.DB.BS_EMP_2
                       on p.MAN_CODE equals p4.MAN_CODE
                       into result2
                       from bb2 in result2.DefaultIfEmpty()
                       where filter.Contains(p.IS_USE)// p.IS_USE == "T"
                       select new { 
                           MAN_CODE=p.MAN_CODE,
                           MAN_NAME = p.MAN_NAME,
                           CHILD_DEPT =p.CHILD_DEPT,
                           DEPT_CODE=p.DEPT_CODE,
                           ID = p.ID,
                           JOB_KIND = p.JOB_KIND,
                           SEX=p.SEX,
                           ARCHIVES_NO = p.ARCHIVES_NO,
                           DRIVE_NO = p.DRIVE_NO,
                           DRIVE_KIND = p.DRIVE_KIND,
                           IN_DATE = p.IN_DATE,
                           class_level = p.class_level,
                           IS_USE=p.IS_USE,
                           IS_CARD=p.IS_CARD=="T"?true:false,
                           IS_CARD_CN = p.IS_CARD == "T" ? "是" : "否"
                           //has_photo = bb.nums==null?false:bb.nums>0
                       };
            if (type=="Moto")
            {
                list = list.Where(a => a.DRIVE_KIND.IndexOf("摩托")>=0||a.DRIVE_KIND=="E");
            }
            else
            {
                list = list.Where(a => !(a.DRIVE_KIND.IndexOf("摩托") >= 0 || a.DRIVE_KIND == "E"));
            }
            
            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.CHILD_DEPT equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret, QuerySettings.DriverList);
        }

        public static IQueryable GetCarMileList(this WebViewPage page)
        {
            string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            var list = from p in WebcarDataProvider.DB.BS_CAR_MILE
                       join p2 in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.CAR_NO equals p2.CAR_NO
                       where p2.IS_USE=="T"
                       select new
                       {
                           dept_code = p2.DEPT_CODE,
                           child_dept = p2.CHILD_DEPT,
                           month = p.NY,
                           car_no = p.CAR_NO,
                           record_no = p2.RECORD_NO,
                           mile = p.MILE,
                           fuel_num = p.FUEL_NUM,
                           fuel_ration = p.FUEL_RATION,
                           fact_ration = 0,
                           read_mile = p.READ_MILE
                       };
            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.child_dept equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret,QuerySettings.CarMileList);
        }
        public static IQueryable GetCarCostList(this WebViewPage page)
        {
            string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            var list = from p in WebcarDataProvider.DB.CarCost
                       join p2 in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.carno equals p2.CAR_NO
                       where p2.IS_USE=="T"
                       select new
                       {
                           //id = p.carno + p.month,
                           dept_code=p.dept_Code,
                           month = p.ny,
                           car_no = p.carno,
                           record_no=p2.RECORD_NO,
                           driver=p.driver,
                           zs_fee=p.zs_fee,
                           cl_fee = p.cl_fee,
                           lq_fee=p.lq_fee,
                           xc_fee=p.xc_fee,
                           tc_fee=p.tc_fee,
                           ns_fee=p.ns_fee,
                           lq_year=p.lq_year,
                           lq_month=p.lq_month,
                           road_fee=p.road_fee,
                           oil_fee=p.oil_fee,
                           other_fee=p.other_fee,
                           oil_nums = p.oil_nums,
                           oil_price =p.oil_price,
                           sum_fee=p.sum_fee
                       };
            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.dept_code equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret,QuerySettings.CarCostList);
        }
        public static IQueryable GetRepairList(RouteData data)
        {
            string dept_code = data.Values["id"].ToSafeString();
            //string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            var list = from p in WebcarDataProvider.DB.RP_CAR_REPAIR_HEAD
                       join car in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.CAR_NO equals car.CAR_NO
                       join user in WebcarDataProvider.DB.BS_EMP
                       on p.DRIVER equals user.MAN_CODE
                        into result
                       from bb in result.DefaultIfEmpty()
                       select new { 
                           repair_no=p.REPAIR_NO,
                           car_no=p.CAR_NO,
                           record_no=car.RECORD_NO,
                           repair_dept=p.REPAIR_DEPT,
                           driver = p.DRIVER,
                           driver_name = bb.MAN_NAME==null?p.DRIVER:bb.MAN_NAME,
                           dept_code=p.DEPT_CODE,
                           enter_date=p.ENTER_DATE,
                           out_date=p.OUT_DATE,
                           repair_kind = p.REPAIR_KIND,
                           hour_fee=p.HOUR_FEE,
                           manage_fee=p.MANAGE_FEE,
                           m_fee=p.SUM_FEE-p.HOUR_FEE-p.MANAGE_FEE,
                           sum_fee=p.SUM_FEE,
                           mile=p.MILE,
                           repair_cause=p.REPAIR_CAUSE,
                           child_dept=car.CHILD_DEPT,
                           checkman_dx=p.CHECKMAN_DX,
                           checkman=p.CHECKMAN
                       };

            IQueryable<v_sys_dept_all> select_depts = GetSelectedDept(dept_code);
            var ret = from p in list
                      join p2 in select_depts on p.child_dept equals p2.deptcode
                      select p;
            return UserQueryBuilder.AppendQuery(ret, QuerySettings.RepairList);
        }
        public static IQueryable GetSloganList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.slogan_info
                       select p;
            return list;
        }
        public static IQueryable GetWorkDynamicList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.work_dynamic
                       select p;
            return list;
        }
        public static IQueryable GetWarningList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.warn_info
                       select p;
            return list;
        }
        public static IQueryable GetContractList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.contract_info
                       select p;
            return UserQueryBuilder.AppendQuery(list,QuerySettings.ContractList);
        }
        public static IQueryable GetGpsList(this RouteData data)
        {
            string dept_code = data.Values["id"].ToSafeString();
            var dept_list = GetSelectedDept(dept_code);
            var list = from p in WebcarDataProvider.DB.bs_gps_summary
                       join p2 in  dept_list
                       on p.DEPT_CODE equals p2.deptcode
                       select new{
                           ny=p.NY,
                        dept_code=p.DEPT_CODE,
                        dept_name=p2.deptname,
                        total_gps=p.TOTAL_GPS,
                        cs_car_num=p.CS_CAR_NUM,
                        cs_num=p.CS_NUM,
                        yj_car_num=p.YJ_CAR_NUM,
                        yj_num=p.YJ_NUM,
                        cs_stop_num = p.CS_STOP_NUM,
                        yj_stop_num = p.YJ_STOP_NUM,
                        cf_num = p.CF_NUM,
                        cf_zj_num=p.CF_ZJ_NUM,
                        note=p.NOTE
                        };
            return UserQueryBuilder.AppendQuery(list,QuerySettings.GpsList);
        }
        public static IQueryable GetRunRecordList(this WebViewPage page)
        {
            string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            var dept_list = GetSelectedDept(dept_code);
            var list = from p in WebcarDataProvider.DB.bs_car_runrecord
                       join p2 in dept_list
                       on p.dept_code equals p2.deptcode
                       join car in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.car_no equals car.CAR_NO
                       select new
                       {
                           xh = p.xh,
                           dept_code = p.dept_code,
                           dept_name = p2.deptname,
                           car_no = p.car_no,
                           record_no=car.RECORD_NO,
                           driver_name = p.driver_name,
                           cetify_man=p.cetify_man,
                           last_date=p.last_date,
                           last_place=p.last_place,
                           run_date = p.run_date,
                           run_place = p.run_place,
                           dp=p.dp,
                           sc=p.sc,
                           yy=p.yy,
                           dq=p.dq,
                           qt=p.qt
                       };
            return UserQueryBuilder.AppendQuery(list,QuerySettings.RunRecordList);
        }
        public static IQueryable GetReportList(this WebViewPage page)
        {
            string dept_code = page.ViewContext.RouteData.Values["id"].ToSafeString();
            var dept_list = GetSelectedDept(dept_code);
            var list = from p in WebcarDataProvider.DB.SEC_MONTH_REPORT
                       join p2 in dept_list
                       on p.DEPT_CODE equals p2.deptcode
                       join p3 in WebcarDataProvider.DB.report_view_response.Where(a=>a.opercode==CurrentUser.LoginUserID&&a.table_name=="report")
                       on p.XH equals p3.table_id
                       into result
                       from bb in result.DefaultIfEmpty()

                       select new { 
                           xh=p.XH,
                           dept_code=p.DEPT_CODE,
                           dept_name=p2.deptname,
                           ny=p.NY,
                           content=p.CONTENT,
                           report_kind = p.REPORT_KIND == "P" ? "车管工作汇报" : "安全学习内容",
                           row_num=p.ROW_NUM,
                           man_name=p.man_name,
                           is_new = bb.opercode==null?"T":"F"
                       };
            return UserQueryBuilder.AppendQuery(list, QuerySettings.ReportList);      
        }
        public static IQueryable GetSendMailList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.SEND_MSG_HEAD
                       where p.WRITER == CurrentUser.LoginUserID
                       select new
                       {
                           msg_xh = p.MSG_XH,
                           note = p.NOTE,
                           send_time = p.SEND_TIME,
                           wh = p.wh,
                           filename = p.filename
                       };
            return UserQueryBuilder.AppendQuery(list,QuerySettings.SendMailList);
        }
        public static IQueryable GetMailList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.SEND_MSG_HEAD
                       join p2 in WebcarDataProvider.DB.SEND_MSG_LOG
                       on p.MSG_XH equals p2.MSG_XH
                       join user in WebcarDataProvider.DB.v_all_users
                       on p.WRITER equals user.account
                       into result
                       from bb in result.DefaultIfEmpty()
                       where p2.MAN_CODE == CurrentUser.LoginUserID
                       select new
                       {
                           msg_xh = p.MSG_XH,
                           man_code = p.WRITER,
                           note = p.NOTE,
                           send_time = p.SEND_TIME,
                           wh = p.wh,
                           filename = p.filename,
                           is_view = p2.IS_VIEW=="T"?true:false,
                           man_name=bb.name==null?p2.MAN_CODE:bb.name,
                           response_note = p2.RESPONSE_NOTE,
                           response_date = p2.RESPONSE_DATE
                       };
            return UserQueryBuilder.AppendQuery(list,QuerySettings.MailList);
        }
        public static IQueryable GetFileResponseList(this WebViewPage page)
        {
            int fileid = page.ViewContext.RouteData.Values["id"].ToSafeString().ToInt();
            var list = from p in WebcarDataProvider.DB.user_view_response
                       join p2 in WebcarDataProvider.DB.v_all_users
                       on p.opercode equals p2.account
                       into result
                       from bb in result.DefaultIfEmpty()
                       where p.table_name == "file" && p.table_id == fileid
                       select new
                       {
                           user_name=bb.name,
                           response_date = p.response_date,
                           response_note = p.response_note
                       };
            return list;
        }
        public static IQueryable GetFileList(this WebViewPage page)
        {
            string file_type = page.ViewContext.RouteData.Values["id"].ToSafeString();
            file_type = file_type.IsNull("FILE");
            var view_list = from p in WebcarDataProvider.DB.user_view_response
                            where p.table_name == "file"
                            group p by p.table_id into result
                            select new { table_id = result.Key, nums = result.Count() };


            var list = from p in WebcarDataProvider.DB.bs_files
                       join p2 in WebcarDataProvider.DB.v_all_users
                       on p.Oper_Code equals p2.account
                       into result
                       from bb in result.DefaultIfEmpty()
                       join p3 in view_list
                       on p.FileId equals p3.table_id
                       into views_result
                       from bb2 in views_result.DefaultIfEmpty()
                       where p.file_type == file_type
                       select new
                       {
                           FileId = p.FileId,
                           FileName = p.FileName,
                           FileName2 = p.FileName2,
                           FileLen = p.FileLen,
                           FileDate = p.FileDate,
                           OperCode = p.Oper_Code,
                           Kind = p.Kind,
                           Note = p.Description,
                           OperName = bb.name,
                           IsNew = bb2.nums==null?true:false
                       };
            //return list;
            return UserQueryBuilder.AppendQuery(list, QuerySettings.FileList);
        }
        public static IQueryable GetUserList(this WebViewPage page)
        {
            IQueryable list = null;
            if (CurrentUser.IsSuperUser)
            {
                list = from p in WebcarDataProvider.DB.v_all_users select p;
            }
            else
            {
                list = from p in WebcarDataProvider.DB.v_all_users
                       join p2 in CurrentUser.UserGrantedDept
                       on p.ori_dept equals p2.deptcode
                       select p;
            }
            return UserQueryBuilder.AppendQuery(list,QuerySettings.UserList);
        }
        public static IQueryable GetDeptList(this WebViewPage page)
        {
            var list = from p in WebcarDataProvider.DB.sys_dept
                              join p2 in WebcarDataProvider.DB.sys_cqep_dept
                              on p.cqep_dept equals p2.deptcode
                              into result
                              from bb in result.DefaultIfEmpty()
                              where p.super_dept == null //&& p.use_flag == "T"
                              select new
                              {
                                  deptcode = p.deptcode,
                                  dept_kind = p.dept_kind,
                                  deptname = p.deptname,
                                  short_name = p.short_name,
                                  cqep_dept_name = bb.deptname,
                                  use_flag = p.use_flag
                              };
            if (!CurrentUser.IsSuperUser)
            {
                list = from p in list
                       join p2 in CurrentUser.UserGrantedDept
                           on p.deptcode equals p2.deptcode
                       select p;
            }
            
            return UserQueryBuilder.AppendQuery(list,QuerySettings.DeptList);
        }

        public static IQueryable<v_sys_dept_all> GetSelectedDept(string dept_code)
        {
            IQueryable<v_sys_dept_all> select_depts = null;
            if (dept_code.IsNull())
            {
                select_depts = CurrentUser.UserGrantedDept;
            }
            else
            {
                select_depts = WebcarDataHelper.GetChildDeptByDept(dept_code);
            }
            return select_depts;
        }


        
        public static DataTable ToDataTable(this IQueryable query)
        {
            DataTable dtReturn = new DataTable();
            PropertyInfo[] columnProperties = null;

            foreach (var record in query)
            {
                // use reflection to get column names for the table, only first time, others will follow
                if (columnProperties == null)
                {
                    columnProperties = record.GetType().GetProperties();
                    foreach (PropertyInfo propertyInfo in columnProperties)
                    {
                        // sort out the issue of nullable types
                        Type columnType = propertyInfo.PropertyType;
                        if ((columnType.IsGenericType) && (columnType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            columnType = columnType.GetGenericArguments()[0];
                        }

                        // add a column to the table
                        dtReturn.Columns.Add(new DataColumn(propertyInfo.Name, columnType));
                    }
                }

                DataRow dataRow = dtReturn.NewRow();

                foreach (PropertyInfo propertyInfo in columnProperties)
                {
                    dataRow[propertyInfo.Name] = propertyInfo.GetValue(record, null) == null ? DBNull.Value : propertyInfo.GetValue(record, null);
                }

                dtReturn.Rows.Add(dataRow);
            }

            return dtReturn;
        }
    }
}